package com.demo.mvp.view;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.demo.activity.BaseActivity;
import com.demo.ioc.annotation.InjectView;
import com.demo.iocandmvp.R;
import com.demo.mvp.bean.UserBean;
import com.demo.mvp.presenter.ILoginPresenter;
import com.demo.mvp.presenter.LoginPresenter;

/**
 * 登陆
 * @author Administrator
 *
 */
public class LoginActivity extends BaseActivity implements LoginView
{
	@InjectView(id = R.id.progress)
	private ProgressBar progressBar;
	
	@InjectView(id = R.id.username)
	private EditText username;
	
	@InjectView(id = R.id.password)
	private EditText password;
	
	@InjectView(id=R.id.button,click="login")
	private Button btn_login;
	
	@InjectView(id=R.id.regist,click="regist")
	private Button btn_regist;

	private ILoginPresenter presenter;
	
	private Animation shake;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		presenter = new LoginPresenter(this);
		//加载动画资源文件
		shake = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.shake);
	}

	@Override
	public void showProgress()
	{
		progressBar.setVisibility(View.VISIBLE);
	}

	@Override
	public void hideProgress()
	{
		progressBar.setVisibility(View.GONE);
	}

	@Override
	public void setUsernameError()
	{
		username.setError(getString(R.string.username_error));
		//给组件播放动画效果
		username.startAnimation(shake);
	}

	@Override
	public void setPasswordError()
	{
		password.setError(getString(R.string.password_error));
		//给组件播放动画效果
		password.startAnimation(shake);
	}

	@Override
	public void navigateToHome()
	{
		Toast.makeText(this, "AAAA", Toast.LENGTH_SHORT).show();
	}

	public void login(){
		presenter.validateCredentials(username.getText().toString(), password.getText().toString());
	}
	
	public void regist(){
		UserBean userBean = new UserBean();
		//userBean.setId(10);
		userBean.setName(username.getText().toString());
		userBean.setPassword(password.getText().toString());
		presenter.regist(userBean);
	}
}
